package com.evolutioninc.wordfinder;

import java.util.*;

public final class WordsSolver {

	// For hangman find match word
	//TODO implement already guess string
	public static ArrayList<String> getMatchingWords(String regex, String notChar){
		int wordLen = regex.length();
		
		if (0 == wordLen)
			return null;
		
		if (regex.matches("[^a-zA-z]"))
			return null;
		
		ArrayList<String> found = new ArrayList<String>();

		// Convert char
		regex = regex.replace('?', '.');
		regex = regex.replace('-', '.');
		regex = regex.replace('*', '.');
		
		String guessLetters = regex.replaceAll("\\.", "");
		String oldLetters = guessLetters;
		if(notChar.length() > 0 && !notChar.matches("[^a-zA-z]")){
			guessLetters += notChar;
		}
			
		regex = regex.replaceAll("\\.", "[^" + guessLetters + "]"); 
		
		for (Map.Entry<String, List<String>> entry : StringUtil.dictionary.get(wordLen).entrySet()) {
		    String key = entry.getKey();
		    
			if (!StringUtil.compareStr(key, oldLetters))
				continue;

			List<String> list = entry.getValue();
			for (String word : list) {
				if (word.matches(regex))
					found.add(word);
			}
		}
		
		return found;
	}
}

